using UnityEngine;
using System.Collections;

public class Game2 : MonoBehaviour {

	
	GUIStyle style = new GUIStyle();
	
	
	float textPosY = Screen.height/2 - 250;
	float textPosX = Screen.width/2 - 250;
	
	string keyToType;
	int keysToWin;
	int random;
	int clock = 0;
	
	bool gameOver = false;
		
	
	private ArrayList  letters;
	
	
	// Use this for initialization
	void Start () {
		letters = new ArrayList();
		letters.Add("A");
		letters.Add("B");
		letters.Add("C");
		letters.Add("D");
		letters.Add("E");
		letters.Add("F");
		letters.Add("G");
		letters.Add("H");
		letters.Add("I");
		letters.Add("J");
		letters.Add("K");
		letters.Add("L");
		letters.Add("M");
		letters.Add("N");
		letters.Add("O");
		letters.Add("P");
		letters.Add("Q");
		letters.Add("R");
		letters.Add("S");
		letters.Add("T");
		letters.Add("U");
		letters.Add("V");
		letters.Add("W");
		letters.Add("X");
		letters.Add("Y");
		letters.Add("Z");		
		newKeySet();
	}
	
	// Update is called once per frame
	void Update () {
		if(!gameOver){
			if (clock > 90){
				Debug.Log("GAME OVER");
				gameOver = true;
			}
			else{
				clock ++;
			}
			switch (keyToType){
				case "A":
					if(Input.GetKeyDown(KeyCode.A)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "B":
					if(Input.GetKeyDown(KeyCode.B)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "C":
					if(Input.GetKeyDown(KeyCode.C)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "D":
					if(Input.GetKeyDown(KeyCode.D)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "E":
					if(Input.GetKeyDown(KeyCode.E)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "F":
					if(Input.GetKeyDown(KeyCode.F)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "G":
					if(Input.GetKeyDown(KeyCode.G)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "H":
					if(Input.GetKeyDown(KeyCode.H)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "I":
					if(Input.GetKeyDown(KeyCode.I)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "J":
					if(Input.GetKeyDown(KeyCode.J)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "K":
					if(Input.GetKeyDown(KeyCode.K)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "L":
					if(Input.GetKeyDown(KeyCode.L)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "M":
					if(Input.GetKeyDown(KeyCode.M)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "N":
					if(Input.GetKeyDown(KeyCode.N)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "O":
					if(Input.GetKeyDown(KeyCode.O)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "P":
					if(Input.GetKeyDown(KeyCode.P)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "Q":
					if(Input.GetKeyDown(KeyCode.Q)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "R":
					if(Input.GetKeyDown(KeyCode.R)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "S":
					if(Input.GetKeyDown(KeyCode.S)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "T":
					if(Input.GetKeyDown(KeyCode.T)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "U":
					if(Input.GetKeyDown(KeyCode.U)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "V":
					if(Input.GetKeyDown(KeyCode.V)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "W":
					if(Input.GetKeyDown(KeyCode.W)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "X":
					if(Input.GetKeyDown(KeyCode.X)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "Y":
					if(Input.GetKeyDown(KeyCode.Y)){
						clock = 0;	
						newKeySet();
					}
				break;
				case "Z":
					if(Input.GetKeyDown(KeyCode.Z)){
						clock = 0;	
						newKeySet();
					}
				break;			
			}
		}
		else{
			if(Input.GetKeyDown(KeyCode.Escape)){
				Application.LoadLevel("Menu");	
			}
		}
	}
	
	// Function to set a new key to be pressed
	// and check if any more key can be used
	void newKeySet (){		
		keysToWin = letters.Count;
		if(keysToWin > 0){
			random = Random.Range(0, keysToWin);
			keyToType = (string) letters[random];			
			letters.RemoveAt(random);
		}else{
			Debug.Log("VENCEU");
			Application.LoadLevel("Winner");
		}
	}
	
	
	
	void OnGUI () {
		if(!gameOver){
			style.fontSize = 48;
	    	style.normal.textColor=Color.white;
	    	style.alignment=TextAnchor.MiddleCenter;
			GUI.Label (new Rect (textPosX, textPosY, 500, 300), 
			"Press key"+"  "+keyToType			
			,style);
		}else{
			style.fontSize = 72;
	    	style.normal.textColor=Color.white;
	    	style.alignment=TextAnchor.MiddleCenter;
			GUI.Label (new Rect (textPosX, 50, 500, 300), 
			"GAME OVER \n"+
			"You typed "+(26-keysToWin)+" correct \n \n "+
			"Press Esc to restart"
			,style);
		}
		
	}
}